package com.sakura.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Main {
    public static void main(String[] args) {
        //创建generator对象
        AutoGenerator autoGenerator = new AutoGenerator();

        //配置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/demo_manage" +
                "?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");

        autoGenerator.setDataSource(dataSourceConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); //指定放置路径
        globalConfig.setOpen(false);  //创建好后是否需要打开文件
        globalConfig.setAuthor("Alan");    //设置作者名
        globalConfig.setServiceName("%sService");//设置不加I

        autoGenerator.setGlobalConfig(globalConfig);

        //包信息
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.sakura.demo");   //设置父包
//        packageConfig.setModuleName("generator");   //在父包下创建一个generator包用来存放
        packageConfig.setController("controller");   //设置Controller放置的包名
        packageConfig.setService("service");   //设置Service放置的包名
        packageConfig.setServiceImpl("serviceImpl");   //设置ServiceImpl放置的包名
        packageConfig.setMapper("mapper");   //设置Mapper放置的包名
        packageConfig.setEntity("bean");   //设置Entity放置的包名

        autoGenerator.setPackageInfo(packageConfig);

        //配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setEntityLombokModel(true);  //在实体类中添加Lombok注解
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//将表中的列名在转换成实体类属性时按照驼峰命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);  //将表中的列名在转换成实体类属性时按照驼峰命名
        //strategyConfig.setInclude("user","product");   //该设置是设置自动生成只生成user和product表的代码
        autoGenerator.setStrategy(strategyConfig);

        //执行
        autoGenerator.execute();

    }
}
